﻿using Syspetro.Core.AppDeal;
using Syspetro.Core.CommandBus;
using Syspetro.Core.Resources;
using Syspetro.Core.SysInterface;
using Syspetro.Deploy.Managers;
using Syspetro.DepRedis;

namespace BJ.AuthDomain.Commands
{
    /// <summary>
    /// 邮箱验证码校验
    /// </summary>
    public class EmailVerifyCommand : Command
    {
        public string Captcha { get; set; }
        public string Email { get; set; }
    }
    public interface IEmailVerifyCommandHandler : ICommandHandler<EmailVerifyCommand>, ITransientService
    {

    }

    public class EmailVerifyCommandHandler : CommandHandler<EmailVerifyCommand>, IEmailVerifyCommandHandler
    {
        readonly IAuthorizationManager _authorization;
        public EmailVerifyCommandHandler(IAuthorizationManager authorization)
        {
            _authorization = authorization;
        }

        public override SpActionResult<string> Execute(EmailVerifyCommand command)
        {
            if (string.IsNullOrEmpty(command.Email))
                command.Email = _authorization.GetEmil;
            if (string.IsNullOrEmpty(command.Email))
                return new SpActionResult<string> { Success = false, Msg = "邮箱为空" };
            RedisHelper redis = new("EmailCode");
            var _code = redis.StringGet(command.Email);
            if (string.IsNullOrEmpty(command.Captcha))
            {
                return new SpActionResult<string> { Success = false, Msg = SharedResource.VerifyCannotEmpty };
            }
            else if (_code == command.Email)
            {
                return new SpActionResult<string> { Success = true, Msg = SharedResource.VerifyCodeOk};
            }
            else
            {
                return new SpActionResult<string> { Success = false, Msg = SharedResource.VerifyCodeError};
            }
        }
    }
}
